package com.example.module_park.enterprise

import com.example.lib_common.base.BaseViewModel
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.RequestEvent
import com.example.lib_common.base.state.Success
import com.example.module_park.model.EnterpriseEntity
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class EnterpriseViewModel @Inject constructor(private val repository:EnterpriseRepository):BaseViewModel() {

    val enterpriseList = mutableListOf<EnterpriseEntity>()
    private val _mEnterprise = MutableStateFlow<RequestEvent>(Loading)
    val enterpriseState get() = _mEnterprise.asStateFlow()


    lateinit var enterpriseDetail: EnterpriseEntity
    private val _mEnterpriseDetail = MutableStateFlow<RequestEvent>(Loading)
    val enterpriseDetailState get() = _mEnterpriseDetail.asStateFlow()

    //所有企业列表
    fun getEnterpriseList() {
        execute(
            action = repository.getEnterpriseList(),
            onStart = {
                _mEnterprise.value = Loading
            },
            onError = {
                _mEnterprise.value = Failed(it)
            },
            onFinish = {
                enterpriseList.clear()
                enterpriseList.addAll(it.data)
                _mEnterprise.value = Success
            }
        )
    }
    fun getEnterpriseDetail(id: Int) {
        execute(
            action = repository.getEnterpriseDetail(id),
            onStart = {
                _mEnterpriseDetail.value = Loading
            },
            onError = {
                _mEnterpriseDetail.value = Failed(it)
            },
            onFinish = { result ->
                enterpriseDetail=result.data
                _mEnterpriseDetail.value = Success
            }
        )
    }

}